package Actors

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.scenes.scene2d.Actor
import floatWidth

class Mario(mX: Float, mY: Float, imgs: Array<out Array<out TextureRegion>>, flipImgs: Array<out Array<out TextureRegion>>) : Actor()
{
    private var stateTime = 0f
    private var animLeft: Animation
    private var animRight: Animation
    private var animLeftIdle: Animation
    private var animRightIdle: Animation
    var state: STATE
    private var frame: TextureRegion? = null
    private val sound by lazy { Gdx.audio.newSound(Gdx.files.internal("step.wav")) }
    private var lastPlayTime = 0L

    init
    {
        x = mX
        y = mY
        state = STATE.RIGHT_IDLE
        animLeft = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(flipImgs[0][0])
            add(flipImgs[0][1])
            add(flipImgs[0][2])
        })
        animRight = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(imgs[0][0])
            add(imgs[0][1])
            add(imgs[0][2])
        })
        animLeftIdle = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(flipImgs[0][0])
        })
        animRightIdle = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(imgs[0][0])
        })
    }


    private fun update()
    {
        stateTime += Gdx.graphics.deltaTime

        when (state)
        {
            STATE.LEFT ->
            { // 往左走，横坐标往左移动
                x -= 1.5f
                if (x <= 20)
                {
                    x = 20f
                }
                frame = animLeft.getKeyFrame(stateTime, true)
                if (System.currentTimeMillis() - lastPlayTime >= 300)
                {
                    sound.play()
                    lastPlayTime = System.currentTimeMillis()
                }
            }
            STATE.RIGHT ->
            {
                // 往右走，横坐标往右移动
                x += 1.5f

                if (x >= Gdx.graphics.floatWidth - (frame?.regionWidth ?: 0) - 20)
                {
                    x = Gdx.graphics.floatWidth - (frame?.regionWidth ?: 0) - 20
                }
                frame = animRight.getKeyFrame(stateTime, true)

                if (System.currentTimeMillis() - lastPlayTime >= 300)
                {
                    sound.play()
                    lastPlayTime = System.currentTimeMillis()
                }
            }
            STATE.LEFT_IDLE ->
            {
                frame = animLeftIdle.getKeyFrame(stateTime, true)
            }
            STATE.RIGHT_IDLE ->
            {
                frame = animRightIdle.getKeyFrame(stateTime, true)
            }
        }
    }

    override fun draw(batch: SpriteBatch, parentAlpha: Float)
    {
        update()
        frame?.let {
            batch.draw(it, x, y)
        }
        super.draw(batch, parentAlpha)
    }

    enum class STATE
    {
        /**
         * 往左走
         */
        LEFT,
        /**
         * 往右走
         */
        RIGHT,
        /**
         * 往左停
         */
        LEFT_IDLE,
        /**
         * 往右停
         */
        RIGHT_IDLE
    }
}
